package dry.example.disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.beans.BeanUtils;

/**
 * 生产者
 *
 * @author zc
 */
public class Producer {

    private final Disruptor<Message> disruptor;

    public Producer(Disruptor<Message> disruptor) {
        this.disruptor = disruptor;
    }

    public void send(Message data) {
        RingBuffer<Message> ringBuffer = this.disruptor.getRingBuffer();
        long next = ringBuffer.next();
        try {
            Message message = ringBuffer.get(next);
            BeanUtils.copyProperties(data, message);
        } finally {
            ringBuffer.publish(next);
        }
    }

}
